package cn.jtcoding.user.service.impl;

import cn.jtcoding.common.framework.enums.YesOrNo;
import cn.jtcoding.user.api.UserAddressService;
import cn.jtcoding.user.mapper.UserAddressMapper;
import cn.jtcoding.user.pojo.bo.UserAddressBO;
import cn.jtcoding.user.pojo.dataobject.UserAddressDO;
import io.seata.spring.annotation.GlobalTransactional;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private Sid sid;

    @GlobalTransactional
    @Override
    public List<UserAddressDO> listUserAddress(String userId) {
        return userAddressMapper.listUserAddress(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addUserAddress(UserAddressBO addressBO) {
        // 1. 判断当前用户是否存在地址，如果没有，则新增为‘默认地址’
        boolean isExists = userAddressMapper.existsDefaultAddress(addressBO.getUserId());

        String addressId = sid.nextShort();
        // 2. 保存地址到数据库
        UserAddressDO newAddress = new UserAddressDO();
        BeanUtils.copyProperties(addressBO, newAddress);
        newAddress.setId(addressId);
        newAddress.setIsDefault(isExists ? YesOrNo.NO.type : YesOrNo.YES.type);
        newAddress.setCreatedTime(new Date());
        newAddress.setUpdatedTime(new Date());

        userAddressMapper.addUserAddress(newAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddress(UserAddressBO addressBO) {
        UserAddressDO pendingAddress = new UserAddressDO();
        BeanUtils.copyProperties(addressBO, pendingAddress);
        pendingAddress.setId(addressBO.getAddressId());
        pendingAddress.setUpdatedTime(new Date());

        userAddressMapper.updateUserAddress(pendingAddress);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void deleteUserAddress(String userId, String addressId) {
        userAddressMapper.deleteUserAddress(userId, addressId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserAddressToBeDefault(String userId, String addressId) {
        // 1. 查找默认地址，设置为不默认
        UserAddressDO queryAddress = userAddressMapper.queryDefaultAddress(userId);
        queryAddress.setIsDefault(YesOrNo.NO.type);

        // 2. 根据地址id修改为默认的地址
        userAddressMapper.updateAddressToBeDefault(userId, addressId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserAddressDO queryUserAddress(String userId, String addressId) {
        return userAddressMapper.queryUserAddress(userId, addressId);
    }
}
